package yl.hs.bmipfull.service.system.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.IAreaDao;
import yl.hs.bmipfull.pojo.system.AreaEntity;
import yl.hs.bmipfull.service.system.IAreaService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.List;
import java.util.Map;

/**
 * 作用:  行政区域服务层接口实现<br/>
 * 创建时间: 2021年05月13日 11时17分29秒 <br/>
 *
 * @author 张凯
 */
@Service
public class AreaServiceImpl extends SystemBaseService implements IAreaService {

    @Autowired
    IAreaDao dao;

    @Override
    public List<AreaEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(AreaEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public List<AreaEntity> queryList() {
        return dao.query();
    }

    @Override
    public List<AreaEntity> queryListFJ(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(AreaEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public AreaEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public List<AreaEntity> findByParentSN(String ByParentSN) {
        return dao.findByParentSN(ByParentSN);
    }

    @Transactional
    public Integer saveArea(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;

        if (Tl.isEmpty(sn)) {
            //执行保存
            AreaEntity entity = new AreaEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(AreaEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入行政区域信息", Tl.toJson(entity), AreaEntity.class);
            }
        } else {
            //执行修改
            AreaEntity entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, AreaEntity.class);
            }
        }
        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {
        Integer result = 0;
        for (String s : sn) {
            result += dao.deleteBySn(s);
        }
        return result;
    }
}